/**
 * This class contains methods each of which corresponds to an encounter type.
 * @author Team Love is Like Sand
 *@version 1.0
 */

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * Class represents an encounter
 * @author kushagramansingh
 * @version 1.0
 */
public class Encounter {
	
	/**
	 * Player to which encounter happens
	 */
	private Player player;
	
	/**
	 * Inventory of player
	 */
	private Inventory inventory;
	
	/**
	 * Frame which actions happen in
	 */
	private JFrame frame;
	
	private MarketController marketController;
	/**
	 * Constructor for Encounter class
	 * @param player Player to be set
	 * @param frame Frame which operatiosn happen in
	 */
	public Encounter(Player player, JFrame frame, MarketController marketController){
		this.marketController = marketController;
		this.player = player;
		this.inventory = player.getInventory();
		this.frame = frame;
		int randEncounter = (int) (Math.random() * 6);
		if(randEncounter >= 0){
			pirateAttack();
		}
		else if(randEncounter == 1){
			policeEncounter();
		}
		else if(randEncounter == 2){
			astroidBelt();
		}
	}
	
	/**
	 * This method handles changes to the inventory caused by
	 *  an encounter with space pirates.
	 */
	public void pirateAttack(){
		BattleController control = new BattleController(player, inventory, frame);
		BattleWindow pirate = new BattleWindow(control,marketController);
	}
	
	/**
	 * This method handles changes to the inventory caused by a police inspection.
	 */
	
	public void policeEncounter(){
		int numLoss = -inventory.getInventory()[3].getNumber();
		inventory.addItem(Inventory.GOODLIST[3], numLoss);
		player.addCargo(numLoss * Inventory.GOODLIST[3].getSize());
		JOptionPane.showMessageDialog(frame, "You were stopped by the police! " +
						"They took all your heroin.");
	}
	
	/**
	 * This method handles the changes in fuel levels 
	 * and cargo caused by having to fly through an asteroid field.
	 */
	public void astroidBelt(){
		double percentLoss = (10.0 - player.getPiloting()) / 100;
		inventory.addMoney((int) (-(inventory.getMoney() * percentLoss)));
		JOptionPane.showMessageDialog(frame, "You hit an asteroid on the way in! " +
										"You paid for repairs.");
	}
	
}
